package com.ocom.mqsimple.business.analyze_access_records;

import com.ocom.common.entity.access.EntityAttendanceRules;
import com.ocom.common.entity.mjrecode.MjAttendance;
import com.ocom.common.entity.mjrecode.MjRecord;
import com.ocom.common.request.access.GetAttendanceRulesInfoReq;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.mqsimple.config.AttendanceConfig;
import com.ocom.mqsimple.feign.AccessClientService;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.MjrecodeClientService;
import com.ocom.redis.util.RedisUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class AttendanceConsumerMqListener {

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    AccessClientService accessClientService;

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    MjrecodeClientService mjrecodeClientService;

    private Logger logger = LoggerFactory.getLogger(AttendanceConsumerMqListener.class);

    @RabbitListener(queues = AttendanceConfig.Attendance_QUEUE)
    public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {

        String requestMessage = new String(message.getBody(), StandardCharsets.UTF_8);

        try {

            Object requestRedis = redisUtil.get(requestMessage);

            if (requestRedis == null) {
                logger.info("Attendance:redis为空异常 Suh7QZkc ");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                return;
            }

            MjRecord mjRecord = (MjRecord) requestRedis;
            //分析

            GetAttendanceRulesInfoReq req =new GetAttendanceRulesInfoReq();
            req.setComId(mjRecord.getComId().longValue());
            req.setAreaId(mjRecord.getAreaId());
            req.setZoneNo(mjRecord.getZoneNo());

            Result<List<EntityAttendanceRules>> entityAttendanceRulesResult =  accessClientService.getAttendanceRuleList(req);
            if(entityAttendanceRulesResult.getCode()!=200){
               throw new RuntimeException("无考勤规则，不用处理");
            }


            for (EntityAttendanceRules entityAttendanceRules:entityAttendanceRulesResult.getData()){

                //休息判断 TODO

                //时间判断  只统计考勤区间内的信息处理 其他用定时任务处理
                String openTime = DateUtil.getStrDate(mjRecord.getOpenTime(),"HH:mm");

                //当天
                if(entityAttendanceRules.getEndTime().compareTo(entityAttendanceRules.getStartTime())>0){
                    if(openTime.compareTo(entityAttendanceRules.getStartTime())<0
                    || openTime.compareTo(entityAttendanceRules.getEndTime())>0){
                        throw new RuntimeException("不在当天考勤区间内");
                    }
                //跨天
                }else{
                    if(openTime.compareTo(entityAttendanceRules.getStartTime())<0
                            && openTime.compareTo(entityAttendanceRules.getEndTime())>0){
                        throw new RuntimeException("不在跨天考勤区间内");
                    }
                }

                MjAttendance mjAttendance =new MjAttendance();
                mjAttendance.setComId(mjRecord.getComId().longValue());
                mjAttendance.setPId(mjRecord.getPId().longValue());
                mjAttendance.setPCode(mjRecord.getPCode());
                mjAttendance.setPName(mjRecord.getPName());
                mjAttendance.setDeptId(mjRecord.getDeptId());
                mjAttendance.setDeptName(mjRecord.getDeptName());
                mjAttendance.setGroupNo(mjRecord.getGroupNo());
                mjAttendance.setGroupName(mjRecord.getGroupName());
                mjAttendance.setRoleId(entityAttendanceRules.getId());
                mjAttendance.setRoleName(entityAttendanceRules.getRuleName());
                mjAttendance.setAttendanceType(entityAttendanceRules.getAttendanceType());
                mjAttendance.setAttendanceDate(mjRecord.getOpenTime());
                mjAttendance.setAttendanceDateTime(mjRecord.getOpenTime());
                mjAttendance.setRecordId(mjRecord.getId());
                mjAttendance.setMonthValue(DateUtil.getStrDate(mjRecord.getOpenTime(),"yyyyMM"));
                mjAttendance.setCreateTime(new Date());
                mjAttendance.setUpdateTime(new Date());

                //提交成功库进行判断---插入或更新
            }

        } catch (Exception e) {
            logger.info("AttendanceConsumerMqListener - -出现异常队列消息开始进入死信队列{{}}", e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
